<template>
  <el-card class="page-container">
    <!-- <template #header>
      <div class="header">
        <span>采购申请</span>
      </div>
    </template> -->
    <div class="app-container">
      <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" >
        <el-form-item label="状态" prop="flag">
          <el-select
              v-model="queryParams.flag"
              clearable
              placeholder="申请状态"
              style="width: 240px"
          >
            <el-option
                v-for="dict in apply_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" style="width: 308px">
          <el-date-picker
              v-model="queryParams.date"
              end-placeholder="结束日期"
              range-separator="-"
              start-placeholder="开始日期"
              type="daterange"
              value-format="YYYY-MM-DD"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              icon="Plus"
              plain
              type="primary"
              @click="handleAdd"
          >发起申请
          </el-button>
        </el-col>
        <!--        <el-col :span="1.5">-->
        <!--          <el-button-->
        <!--              :disabled="single"-->
        <!--              icon="Edit"-->
        <!--              plain-->
        <!--              type="success"-->
        <!--              @click="handleUpdate"-->
        <!--          >修改-->
        <!--          </el-button>-->
        <!--        </el-col>-->
        <!--        <el-col :span="1.5">-->
        <!--          <el-button-->
        <!--              :disabled="multiple"-->
        <!--              icon="Delete"-->
        <!--              plain-->
        <!--              type="danger"-->
        <!--              @click="handleDelete"-->
        <!--          >删除-->
        <!--          </el-button>-->
        <!--        </el-col>-->
      </el-row>

      <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
        <!--        <el-table-column align="center" type="selection" width="55"/>-->
        <!--        <el-table-column align="center" label="序号" prop="id"/>-->
        <el-table-column align="center" label="序号" type="index" width="60">
          <!-- 序号重新计算 -->
          <template #default="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" align="center" label="申请批次" prop="batchName"/>
        <el-table-column :show-overflow-tooltip="true" align="center" label="申请物品" prop="productsName"/>
        <el-table-column :show-overflow-tooltip="true" align="center" label="申请数量" prop="amount"/>
        <el-table-column :show-overflow-tooltip="true" align="center" label="申请金额" prop="price"/>
        <el-table-column :show-overflow-tooltip="true" align="center" label="申请理由" prop="reason"/>
        <!--      <el-table-column :show-overflow-tooltip="true" align="center" label="类型">-->
        <!--        <template #default="scope">-->
        <!--          <router-link :to="'/system/dict-data/index/' + scope.row.dictId" class="link-type">-->
        <!--            <span>{{ scope.row.dictType }}</span>-->
        <!--          </router-link>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <el-table-column align="center" label="状态" prop="flag">
          <template #default="scope">
            <dict-tag :options="apply_status" :value="scope.row.flag"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="160">
          <template #default="scope">
            <el-button v-if="scope.row.flag===2" icon="Edit" link type="primary" @click="handleUpdate(scope.row)">
              修改
            </el-button>
            <el-button v-if="scope.row.flag===2" icon="Delete" link type="primary"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total > 0"
          v-model:limit="queryParams.pageSize"
          v-model:page="queryParams.pageNum"
          :total="total"
          @pagination="getList"
      />

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog v-model="open" :title="title" append-to-body width="700px">
        <el-form ref="dictRef" :model="apply" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="10">
              <el-form-item label="商品名称">
                <el-select v-model="form.productsId" placeholder="请选择" @change="productsprice">
                  <el-option
                      v-for="item in apply"
                      :key="item.productsId"
                      :disabled="item.status == 1"
                      :label="item.productsName"
                      :value="item.productsId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item label="采购批次">
                <el-select v-model="form.batchId" placeholder="请选择">
                  <el-option
                      v-for="item in batch"
                      :key="item.productsId"
                      :disabled="item.status == 1"
                      :label="item.name"
                      :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>


          <el-row>
            <el-col :span="10">
              <el-form-item label="单价(元)" prop="amount">
                <el-input v-model="price" disabled/>
              </el-form-item>
            </el-col>
            <el-col :span="14">
              <el-form-item label="申请数量" prop="amount">
                <el-input v-model="form.amount" placeholder="请输入采购数量" type="number" @input="cateprice"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="总价(元)" prop="amount">
            <el-input v-model="totalprice" disabled/>
          </el-form-item>
          <el-form-item label="申请理由" prop="reason">
            <el-input v-model="form.reason" placeholder="请输入内容" type="textarea"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </el-card>
</template>

<script name="Dict" setup>
import {
  addApply,
  delApply,
  listMyApply,
  priceById,
  stockNoListall,
  updateApply
} from "@/api/productsbuy/applyprodects.js";
import {batchUseableService} from "@/api/stock/batch.js";
// const totalprice = ref();
// const totalprice = price * form.amount;

const {proxy} = getCurrentInstance();
const {sys_normal_disable} = proxy.useDict("sys_normal_disable");
const {apply_status} = proxy.useDict("apply_status");

const applyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    flag: undefined,
    date: [],
    startTime: undefined,
    endTime: undefined
  }
});

const {queryParams, form, rules} = toRefs(data);
const apply = ref([]);
const batch = ref([]);
const price = ref();
const totalprice = ref();


const cateprice = async () => {
  // console.log(price.value);
  // console.log(data.form.amount);
  totalprice.value = price.value * data.form.amount;

}


const productsprice = async () => {
  // console.log("222---", form.value.productsId);
  let r = await priceById({id: form.value.productsId})
  // console.log("batch", r.price);
  price.value = r.price
}

function cate() {
  console.log("----")

}

/** 查询我的申请列表 */
function getList() {
  loading.value = true;
  listMyApply(queryParams.value).then(response => {
    applyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function get() {
  stockNoListall().then(response => {
    apply.value = response.apply;
  });
  batchUseableService().then(response => {
    batch.value = response.data;
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  price.value = '';
  totalprice.value = ''
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    flag: undefined,
    date: [],
    startTime: undefined,
    endTime: undefined
  };
  proxy.resetForm("dictRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.startTime = queryParams.value.date[0];
  queryParams.value.endTime = queryParams.value.date[1];
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.date = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "发起申请";
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = row;
  open.value = true;
  title.value = "修改申请";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dictRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateApply(form.value).then(response => {
          proxy.$modal.msgSuccess(response.msg);
          open.value = false;
          getList();
        });
      } else {
        addApply(form.value).then(response => {
          proxy.$modal.msgSuccess(response.msg);
          open.value = false;
          getList();
        });
      }
    }
    price.value = '';
    totalprice.value = ''
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const applyIds = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除字典编号为"' + applyIds + '"的数据项？').then(function () {
    return delApply(applyIds);
  }).then(response => {
    getList();
    proxy.$modal.msgSuccess(response.msg);
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/dict/type/export", {
    ...queryParams.value
  }, `dict_${new Date().getTime()}.xlsx`);
}


getList();
get();
</script>
